package com.hpu.tools;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;

import com.hpu.constants.ShareConstants;

/**
 * 对sharePreference的封装
 * @author Administrator
 *
 */
public class ShareTools {
	static SharedPreferences preferences;
	static SharedPreferences.Editor editor;

	@SuppressLint("CommitPrefEdits")
	@SuppressWarnings("static-access")
	private static void getSharePreference(Context context){
		if(context!=null){
			if(preferences==null||editor==null)
			preferences = context.getSharedPreferences(ShareConstants.STORE_COURSE, context.MODE_PRIVATE);
			editor = preferences.edit();
		}
	}
	
	@SuppressLint("CommitPrefEdits")
	public static Object get(Context context,String key,Object defaultValue){
		getSharePreference(context);
		if(defaultValue instanceof Integer) return preferences.getInt(key,(int)defaultValue);
		if(defaultValue instanceof String) return preferences.getString(key,(String)defaultValue);
		if(defaultValue instanceof Boolean) return preferences.getBoolean(key,(Boolean)defaultValue);
		if(defaultValue instanceof Float) return preferences.getFloat(key,(Float)defaultValue);
		return defaultValue;
	}
	
	public static void put(Context context,String key,Object value){
		getSharePreference(context);
		if(value instanceof Integer){

			editor.putInt(key,(int) value);
		}
		if(value instanceof String) editor.putString(key,(String) value);
		if(value instanceof Boolean) editor.putBoolean(key,(Boolean) value);
		if(value instanceof Float) editor.putFloat(key,(Float) value);
		editor.commit();
	}
	
	public static void clear(Context context){
		getSharePreference(context);
		editor.clear();
		editor.commit();
	}
}
